<?php
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------

namespace app\controller\admin\v1\building;

use app\controller\admin\AuthController;

use app\controller\admin\Common;
use app\model\v1\building\BuildingMaterialsCategory;
use app\model\v1\recovery\SystemOperationLog;
use app\model\v1\recruit\RecruitJobSeeker;
use app\Request;
use app\services\article\ArticleCategoryServices;
use app\validate\admin\building\materialsValidate;
use crmeb\services\CacheService;
use think\facade\App;
use think\filesystem\driver\Local;
use think\facade\Cache;

/**
 * 建材市场管理
 * Class ArticleCategory
 * @package app\controller\admin\v1\building
 */
class BuildingMaterials extends AuthController
{
    protected $model;

    public function __construct(App $app, RecruitJobSeeker $model)
    {
        parent::__construct($app);
        $this->model = $model;
    }




    /**
     * 主材采购页面 - 分类列表
     * @return mixed
     */
    public function getMaterialsCategoryList(Request $request)
    {
        $param = $request->param();
        $param['page'] = input('param.page', 1);
        $param['limit'] = input('param.limit', 0);

        $data = BuildingMaterialsCategory::getMaterialsCategoryList($param);
        return $this->success($data);
    }


    /**
     * 获取分类树
     * @param Request $request
     * @return mixed
     */
    public function category_tree(Request $request)
    {
        $param = $request->param();
        $materials = '';
        if(!empty($param["materials"])){
            $materials = $param["materials"];
            $mains_id = BuildingMaterialsCategory::where('name', $materials)->whereNull('delete_time')->value('id');

            $cate_ids = Common::getChildren(BuildingMaterialsCategory::select()->toArray(), $mains_id);
            $cate_ids = array_unique($cate_ids);
        }

        if(!empty($materials)){
            $data = BuildingMaterialsCategory::field('id,name,parent_id,sort,visibility,img')->whereIn("id", $cate_ids)->select()->toArray();
        } else{
            $data = BuildingMaterialsCategory::field('id,name,parent_id,sort,visibility,img')->select()->toArray();
        }

        $tree = Common::buildTree($data);
        return $this->success($tree);
    }


    /**
     * 添加建材分类
     * @param Request $request
     * @return mixed
     */
    public function addMaterialsCategory(Request $request)
    {
        $param = $request->postMore([
            ['name', ''],
            ['parent_id', ''],
            ['visibility', ''],
        ]);
        validate(materialsValidate::class)->check($param);

        $admin_id = $this->adminId;
        ////$admin_id = Cache::get('admin_id');

        $buil = new BuildingMaterialsCategory();
        $buil->name         = $request->post('name');
        $buil->parent_id    = $request->post('parent_id');
        $buil->sort         = $request->post('sort');
        $buil->visibility   = $request->post('visibility');
        $buil->img          = $request->post('img');
        $buil->admin_id     = $admin_id;
        $buil->save();

        return $this->success("success");
    }


    /**
     * 编辑建材分类
     * @param Request $request
     * @return mixed
     */
    public function updateMaterialsCategory(Request $request)
    {
        $param = $request->post();
        if(empty($param['id'])) return $this->fail('id不能为空');
        if(empty($param['name'])) return $this->fail('name不能为空');
        if(empty($param['parent_id'])) return $this->fail('parent_id不能为空');
        if(empty($param['visibility'])) return $this->fail('visibility不能为空');

        $param["admin_id"] = $this->adminId;
        ////$param["admin_id"] = Cache::get('admin_id');
        $res = BuildingMaterialsCategory::updateMaterialsCategory($param);

        $admin_id = $this->adminId;
        $ip = $request->ip();
        $desc = "-将id为{$param['id']}的数据编辑为：名称：{$param['name']}.";
        SystemOperationLog::addSystemLog($admin_id,'building','建材分类管理列表','updateMaterialsCategory','编辑',$param,$ip,$desc);

        if($res >= 0){
            return $this->success('编辑成功');
        }
        return $this->fail('编辑失败');
    }


    /**
     * 建材分类管理列表 - 删除
     * @param Request $request
     * @return mixed
     */
    public function deleteMaterialsCategory(Request $request)
    {
        $param = $request->post();
        if(empty($param['id'])) return $this->fail('id不能为空');

        $buil_parent = BuildingMaterialsCategory::where('parent_id', $param['id'])->find();
        if(!empty($buil_parent)){
            return $this->fail('请先删除子分类');
        }

        $res = BuildingMaterialsCategory::where("id", $param['id'])->update(['delete_time'=>time()]);

        $admin_id = $this->adminId;
        $ip = $request->ip();
        $desc = "-将id为{$param['id']}的数据删除.";
        SystemOperationLog::addSystemLog($admin_id,'building','建材分类管理列表','deleteMaterialsCategory','删除',$param,$ip,$desc);

        if ($res >= 0)  return $this->success('删除成功');

        return $this->fail('删除失败');
    }


    /**
     * 建材分类管理列表 - 启用禁用
     * @param Request $request
     * @return mixed
     */
    public function enableMaterialsCategory(Request $request)
    {
        $param = $request->post();
        if(empty($param['id'])) return $this->fail('id不能为空');

        $msg = BuildingMaterialsCategory::enableMaterialsCategory($param);

        return $this->success($msg);
    }


}
